package com.csdn.leon.activity;

import java.util.List;

import com.csdn.leon.adapter.DialogAdapter.ClickedListener;
import com.csdn.leon.adapter.WatchBloggerAdapter;
import com.csdn.leon.base.BaseActivity;
import com.csdn.leon.db.DBUtil;
import com.csdn.leon.model.Person;
import com.csdn.leon.view.dialog.DialogHelper;
import com.csdn.leon.view.dialog.SearchDialog;
import com.csdn.leon.base.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class WatchBloggerActivity extends BaseActivity implements
		OnClickListener {
	public static final String flag = "COM.DIALOG.SEARCH";
	private GridView gridview;
	private WatchBloggerAdapter adapter;
	private List<Person> list;
	private TextView leftTv;
	private ImageView leftImg;
	private TextView middleTv;
	private ImageView search;
	SearchDialog dialog;
	BroadCastReceiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_blogger_layout);
		initView();
		// initData();
		receiver = new BroadCastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(flag); // 只有持有相同的action的接受者才能接收此广播
		registerReceiver(receiver, filter);
	}

	private void initView() {
		gridview = (GridView) findViewById(R.id.gridview);
		adapter = new WatchBloggerAdapter(WatchBloggerActivity.this);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Person person = adapter.getList().get(position);
				Intent intent = new Intent(WatchBloggerActivity.this,
						BlogHostActivity.class);
				intent.putExtra("username", person.getUsername());
				startActivity(intent);
			}
		});
		leftTv = (TextView) findViewById(R.id.title_left_text);
		leftTv.setVisibility(View.GONE);
		leftImg = (ImageView) findViewById(R.id.title_left_img);
		leftImg.setVisibility(View.VISIBLE);
		leftImg.setImageResource(R.drawable.actionbar_back_icon_normal);
		leftImg.setOnClickListener(this);
		middleTv = (TextView) findViewById(R.id.title_middle_text);
		middleTv.setVisibility(View.VISIBLE);
		middleTv.setText(this.getResources().getText(
				R.string.title_middleleft_5));
		search = (ImageView) findViewById(R.id.title_right_search);
		search.setVisibility(View.VISIBLE);
		search.setOnClickListener(this);
	}

	private void initData() {
		new GetPersonListTask(WatchBloggerActivity.this).execute();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}

	private class GetPersonListTask extends AsyncTask<Void, Void, List<Person>> {

		private Context context;

		GetPersonListTask(Context context) {
			this.context = context;
		}

		@Override
		protected List<Person> doInBackground(Void... params) {
			List<Person> list = DBUtil.getHostList(context);

			return list;
		}

		@Override
		protected void onPostExecute(List<Person> list) {
			adapter.setList(list);
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.title_left_img:
			finish();
			break;
		case R.id.title_right_search:
			dialog = DialogHelper.getSearchDialog(WatchBloggerActivity.this);
			dialog.show();
			break;
		}
	}

	public class BroadCastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			dialog.cancel();
		}

	}

}
